/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package zpsb.accounting.session;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
//import javax.persistence.Query;
import javax.persistence.TypedQuery;
import zpsb.accounting.model.User;

/**
 *
 * @author Michal
 */
@Stateless
public class UserBean {

    @PersistenceContext
    EntityManager em;

    public void create(User user) {
        em.persist(user);
    }

    public List<User> getAllUsers() {
        TypedQuery<User> q = em.createQuery("select u from User u", User.class);
        return q.getResultList();
    }

    public User findByUsername(String username) {
        TypedQuery<User> q = em.createQuery("select u from User u where u.username = :username", User.class);
        q.setParameter("username", username);
        try {
            return q.getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }
}
